package com.muh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/providerController")
public class ProviderController {

    @GetMapping("/test1")
    public String test1(@RequestParam(name = "args")String args) {
        System.out.println("providerController传入的值"+args);
        return "传入的值为："+args;
    }
    @GetMapping("/test2")
    public String test2(@RequestParam(name = "args")String args) {
        System.out.println("providerController传入的值"+args);
        return "传入的值为："+args;
    }
    @GetMapping("/test3")
    public String test3(@RequestParam(name = "args")String args) {
        System.out.println("providerController传入的值"+args);
        return "传入的值为："+args;
    }
    @GetMapping("/test4")
    public String test4(@RequestParam(name = "args")String args) {
        System.out.println("providerController传入的值"+args);
        return "传入的值为："+args;
    }
    @GetMapping("/test5")
    public String test5(@RequestParam(name = "args")String args) {
        System.out.println("providerController传入的值"+args);
        return "传入的值为："+args;
    }

    /*********************************************************************************************/
    @GetMapping("/hello1")
    public String helhello1lo() {
        return "hello world";
    }

    @GetMapping("/hello2")
    public String hello2() {
        return "hello world";
    }

    @GetMapping("/hello3")
    public String hello3() {
        return "hello world";
    }

    @GetMapping("/hello4")
    public String hello4() {
        return "hello world";
    }

    /*********************************************************************************************/
    @Autowired
    private DiscoveryClient client;

    /**
     * 使用DiscoveryClient获取当前服务的基本信息
     * @return
     */
    @GetMapping("/getDiscovery")
    public Object getClient() {
        //获取当前服务的实列
        List<String> services = client.getServices();
        services.forEach(item->{
            System.out.println(item);//spring.application.name
        });
        //获取当前服务的实列
        List<ServiceInstance> instances = client.getInstances("provider-server-8001");
        instances.forEach(item->{
            System.out.println(item.getHost()+"\t"+//eureka.instance.hostname   localhost
                    item.getPort() +"\t" +//server.port     8001
                    item.getServiceId()+"\t"+//spring.application.name  一般为大写    PROVIDER-SERVER-8001
                    item.getUri()+"\t"+//自己本身地址+端口       http://localhost:8001
                    item.getInstanceId());//eureka.instance.instance-id        instance-id-provider-server-8001
        });
        return client;
//        页面返回如下
//        {"discoveryClients":[{"services":["provider-server-8001"],"order":0},{"services":[],"order":0}],"services":["provider-server-8001"],"order":0}
    }
}
